package com.flashflexpro.flexdirector.adapter.codec {

import com.flashflexpro.flexdirector.adapter.descriptor.DescPropertyAuto;

import com.flashflexpro.flexdirector.model.CodeStringValue;

import mx.automation.IAutomationObject;
import mx.charts.ChartItem;
import mx.charts.HitData;
import mx.charts.chartClasses.Series;

public class CodecHitData extends CodecDefault{


    override public function encode(obj:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject ):CodeStringValue {
        var rt:CodeStringValue = new CodeStringValue();
        rt.desc = descProperty.getDto();
        var val:Object = getMemberFromObject( obj, descProperty );
        if( val != null && val is HitData ){
            rt.value = ( val as HitData ).chartItem.index;
            rt.string = String(rt.value);
        }
        else{
            rt.value = -1;
            rt.string = "-1";
        }
        rt.code = "" + rt.value + "";
        return rt;
    }

    override public function decode(obj:Object, value:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject, inString:Boolean = false):void {
        if (relativeParent is Series){
    		var series:Object = relativeParent;
    		var items:Array = series.items;
    		for(var i:int = 0; i < items.length; ++i)
    		{
    			if (items[i] is ChartItem)
    			{
    				var chartItem:ChartItem = items[i] as ChartItem;
    				if ( chartItem.index == value)
    				{
   						obj[descProperty.name] =
   									new HitData(0, 0, 0, 0, chartItem);
    					break;
    				}
    			}
    		}
    	}
    }
}
}